package com.lanou.pianke.search.service.impl;

import com.lanou.pianke.search.EntityRepository.ArticleRepository;
import com.lanou.pianke.search.entity.Article;
import com.lanou.pianke.search.service.ArticleService;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {


    @Autowired
    ArticleRepository articleRepository;


    @Override
    public List<Article> searchByArticleContent(String searchContent) {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        Iterable<Article> search = articleRepository.search(builder);
        Iterator<Article> iterator = search.iterator();
        List<Article> articleList = new ArrayList<>();
        while (iterator.hasNext()){
            articleList.add(iterator.next());
        }
        return articleList;
    }
}
